package cycling.taxi.com.ui.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.DecimalFormat;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.R;
import cycling.taxi.com.bean.LocationInfo;
import cycling.taxi.com.bean.UserVerhicle;
import cycling.taxi.com.eventbus.CloseService;
import cycling.taxi.com.eventbus.SendMassage;
import cycling.taxi.com.eventbus.SetTime;
import cycling.taxi.com.ui.activity.HomeAty;
import cycling.taxi.com.ui.activity.LowBatteryAty;
import cycling.taxi.com.ui.activity.MileageAty;
import cycling.taxi.com.ui.activity.TrajectoryList;
import cycling.taxi.com.utils.PowerUtils;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;

/**
 * 车况
 * Created by Administrator on 2017/4/20.
 */
public class ConditionFrgt extends Fragment implements View.OnClickListener {

    //FrameLayout mFrgtHomeFlayoutViewpager;
    @InjectView(R.id.frgt_home_flayout_map)
    FrameLayout mFrgtHomeFlayoutMap;
    @InjectView(R.id.frgt_home_address)
    TextView mFrgtHomeAddress;
    @InjectView(R.id.frgt_home_time)
    TextView mFrgtHomeTime;
    @InjectView(R.id.frgt_home_rtlayout)
    RelativeLayout mFrgtHomeRtlayout;
    @InjectView(R.id.frgt_home_low_battery_tv)
    TextView mLowBatteryTv;//电量tv
    @InjectView(R.id.frgt_home_low_battery_bt)
    RelativeLayout mLowBatteryBt;//电量按钮
    @InjectView(R.id.frgt_home_legend_tv)
    TextView mLegendTv;//里程tv
    @InjectView(R.id.frgt_home_mileage_bt)
    RelativeLayout mLegendBt;//里程按钮
    @InjectView(R.id.frgt_home_top)
    LinearLayout frgtHomeTop;
    @InjectView(R.id.power)
    Button power;
    @InjectView(R.id.tracy)
    Button tracy;
    @InjectView(R.id.progressBar)
    ProgressBar progressBar;
    @InjectView(R.id.linearLayout)
    RelativeLayout linearLayout;
    private BaiduMapFrgt mBaiduMapFrgt;
    private Context mContext;
    float x1 = 0;
    float x2 = 0;
    float y1 = 0;
    float y2 = 0;
    private float mDensity;
    private HomeAty.MyTouchListener myTouchListener = new HomeAty.MyTouchListener() {
        @Override
        public void onTouchEvent(MotionEvent event) {
            // 处理手势事件
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                //当手指按下的时候
                x1 = event.getX();
                y1 = event.getY();
            }
            if(event.getAction() == MotionEvent.ACTION_UP) {
                //当手指离开的时候
                x2 = event.getX();
                y2 = event.getY();
                if(y1 - y2 > 50) {
                    animateClose(frgtHomeTop);
                    animationIvClose();
                }
            }
        }
    };
    private int mHiddenViewMeasuredHeight;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDensity = getResources().getDisplayMetrics().density;
        mHiddenViewMeasuredHeight = (int) (mDensity *400+ 0.5);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.frgt_condition, null);
        mContext = inflater.getContext();
        ButterKnife.inject(this, rootview);
        initUI(rootview);
        initData();
        setListener();
        progressBar.setProgress(Integer.parseInt(String.valueOf(mLowBatteryTv.getText())));
        ((HomeAty)this.getActivity()).registerMyTouchListener(myTouchListener);
        //加载地图
        mBaiduMapFrgt = new BaiduMapFrgt();
        getFragmentManager().beginTransaction().add(R.id.frgt_home_flayout_map, mBaiduMapFrgt).commit();
       // linearLayout.findViewById(R.id.linearLayout);
        // 将myTouchListener注册到分发列表

        return rootview;
    }




    private void initData() {

    }

    private void setListener() {
        power.setOnClickListener(this);
        tracy.setOnClickListener(this);
      //  mIv.setOnClickListener(this);
        linearLayout.setOnClickListener(this);
    }


    private void initUI(View rootview) {
        ButterKnife.inject(this, rootview);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tracy:
                //跳转到里程界面
                 goToAty(TrajectoryList.class);
                break;
            case R.id.power:
                //跳转到电量显示界面
                goToAty(LowBatteryAty.class);
                break;
//            case R.id.my_iv:
//                if (frgtHomeTop.getVisibility() == View.GONE) {
//                    animateOpen(frgtHomeTop);
//                    animationIvOpen();
//
//                } else {
//                    animateClose(frgtHomeTop);
//                    animationIvClose();
//                }
//                break;
            case R.id.linearLayout:
                if (frgtHomeTop.getVisibility() == View.GONE) {
                    animateOpen(frgtHomeTop);
                    
                    animationIvOpen();

                } else {
                    animateClose(frgtHomeTop);
                    animationIvClose();
                }
                break;


        }

    }

    @Subscribe
    public void onEvent(SetTime time){
        frgtHomeTop.setVisibility(View.VISIBLE);
        mFrgtHomeTime.setText(time.getTime().getLastTime());
        mLegendTv.setText(time.getTime().getMileage());
        String verhicledata =SPUtil.getInstance().getData("verhicledata");
        Gson gson = new Gson();
        UserVerhicle userVerhicle= gson.fromJson(verhicledata, UserVerhicle.class);
        PowerUtils powerUtils=new PowerUtils();
        mLowBatteryTv.setText(powerUtils.Power(userVerhicle.getVehicleMotorpower(),time.getTime().getVol()));
        progressBar.setProgress(Integer.parseInt(String.valueOf(mLowBatteryTv.getText())));

    }
    private void goToAty(Class cs) {
        Intent intent = new Intent(mContext,cs);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ((HomeAty)this.getActivity()).unRegisterMyTouchListener(myTouchListener);
        ButterKnife.reset(this);
        EventBus.getDefault().post(new SendMassage());
        EventBus.getDefault().post(new CloseService());
        Log.d("ConditionFrgt","onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }


    private void animateOpen(View v) {
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0,
                mHiddenViewMeasuredHeight);
        animator.start();

    }

    private void animationIvOpen() {
        RotateAnimation animation = new RotateAnimation(0, 180,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        linearLayout.startAnimation(animation);
    }

    private void animationIvClose() {
        RotateAnimation animation = new RotateAnimation(180, 0,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        linearLayout.startAnimation(animation);
    }

    private void animateClose(final View view) {
        int origHeight=50;
        if(view==null){
            origHeight=50;
        }else {
            origHeight = view.getHeight();

        }

        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }

        });
        animator.start();
    }

    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();

                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);

            }
        });
        return animator;
    }


        //((HomeAty)this.getActivity()).unRegisterMyTouchListener(myTouchListener);




}
